package hng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hng.dto.AddressDTO;
import hng.interceptor.LoginInterceptor;
import hng.model.AddressDO;
import hng.mapper.AddressMapper;
import hng.model.LoginUser;
import hng.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hng.vo.AddressVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, AddressDO> implements AddressService {

    @Autowired(required = false)
    private AddressMapper addressMapper;

    /**
     * 添加收获地址
     */
    @Override
    public void add(AddressDTO addressDTO) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
//        LoginInterceptor是一个拦截器，用于拦截用户请求，进行用户登录验证等操作。
//        threadLocal是一个线程局部变量，用于在多线程环境下存储每个线程的局部变量，避免线程安全问题。
        AddressDO addressDO = new AddressDO();
        String addressFull = getAddressFull(addressDTO);
        BeanUtils.copyProperties(addressDTO, addressDO);
        addressDO.setUserId(loginUser.getId());
        addressDO.setAddressFull(addressFull);

        // 如果设置了默认地址，要把其他的地址设置为不是默认的
        if ("1".equals(addressDTO.getIsDefault())) {
            setNotDefault(loginUser.getId());
        }

        addressMapper.insert(addressDO);
    }

    /**
     * 删除收货地址
     */
    @Override
    public void del(Integer id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        int row = addressMapper.delete(new LambdaQueryWrapper<AddressDO>()
                .eq(AddressDO::getId, id)
                .eq(AddressDO::getUserId, loginUser.getId())
        );
    }

    /**
     * 修改收货地址
     */
    @Override
    public void update(AddressDTO addressDTO) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = new AddressDO();
        BeanUtils.copyProperties(addressDTO, addressDO);
        String addressFull = getAddressFull(addressDTO);
        addressDO.setAddressFull(addressFull);

        // 如果设置了默认地址，要把其他的地址设置为不是默认的
        if ("1".equals(addressDTO.getIsDefault())) {
            setNotDefault(loginUser.getId());
        }

        int row = addressMapper.update(addressDO, new LambdaQueryWrapper<AddressDO>()
                .eq(AddressDO::getUserId, loginUser.getId())
                .eq(AddressDO::getId, addressDTO.getId())
        );
    }

    /**
     * 查询收货地址列表
     */
    @Override
    public List<AddressDO> addressList() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<AddressDO> addressDOList = addressMapper.selectList(new LambdaQueryWrapper<AddressDO>()
                .eq(AddressDO::getUserId, loginUser.getId())
                .orderByDesc(AddressDO::getIsDefault)
                .orderByDesc(AddressDO::getId)
        );

        return addressDOList;
    }

    /**
     * 设置为默认地址
     */
    private void setNotDefault(Integer userId) {
        AddressDO addressDO = new AddressDO();
        addressDO.setIsDefault(0);
        int row = addressMapper.update(addressDO, new LambdaQueryWrapper<AddressDO>()
                .eq(AddressDO::getUserId, userId)
        );
    }
    /**
     * 把地址拼接起来
     */
    private String getAddressFull(AddressDTO addressDTO) {
        StringBuilder addressFull = new StringBuilder();
        addressFull.append(addressDTO.getProvince())
                .append(addressDTO.getCity())
                .append(addressDTO.getArea())
                .append(addressDTO.getStreet());
        return addressFull.toString();
    }
}
